package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.controller.form.AddFlowForm;
import com.woniu.controller.form.ModifyFlowForm;
import com.woniu.model.Result;
import com.woniu.service.FlowService;
import com.woniu.service.param.AddFlowParam;
import com.woniu.service.param.ModifyFlowParam;
import com.woniu.util.RedisUtil;
import com.woniu.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  流程表控制类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@Api(tags = "流程相关接口")
@RequestMapping("/flow")
public class FlowController {
    @Resource
    private FlowService flowService;
    @Resource
    private RedisUtil redisUtil;

    /**
     *新增流程表
     */
    @PostMapping("/add.api")
    @ApiOperation("添加流程")
    public Result<?> add(@RequestBody AddFlowForm form, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        int id = Integer.parseInt(JwtUtil.parseAdminToken(refreshToken).get("id").toString());
        AddFlowParam param = BeanUtil.toBean(form, AddFlowParam.class);
        param.setCreateUser(id);
        flowService.add(param);
        return Result.newInstance(null).setCode(200).setMessage("添加流程成功!");
    }

    /**
     *更改流程表
     * @param form 修改form
     */
    @PostMapping("/modify.api")
    @ApiOperation("修改流程")
    public Result<?> modify(@RequestBody ModifyFlowForm form, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        int id = Integer.parseInt(JwtUtil.parseAdminToken(refreshToken).get("id").toString());
        ModifyFlowParam param = BeanUtil.toBean(form, ModifyFlowParam.class);
        param.setUpdateUser(id);
        flowService.modify(param);
        return Result.newInstance(null).setCode(200).setMessage("修改流程成功!");
    }
}

